package com.lv.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lv.pojo.Student;
import com.lv.service.StudentService;
import com.lv.util.MD5;
import com.lv.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.util.List;

/**
 * @Author:晓风残月Lx
 * @Date: 2022/4/28 19:05
 */
@Api(tags = "学生控制器")
@RestController
@RequestMapping("/sms/studentController")
public class StudentController {

    @Autowired
    StudentService studentService;

    @ApiOperation("查询学生信息，带分页")
    @GetMapping("/getStudentByOpr/{pageNo}/{pageSize}")
    public Result getStudentByOper(
              @ApiParam("页码数") @PathVariable("pageNo") Integer pageNo,
              @ApiParam("页码大小") @PathVariable("pageSize") Integer pageSize,
              @ApiParam("查询条件转换后端数据模型") Student student
        ){
        // 准备分页信息封装的page对象
        Page<Student> page=new Page<>(pageNo,pageSize);
        // 获取分页的学生信息
        IPage<Student> pageRs=studentService.getStudentByOpr(page,student);

        return Result.ok(pageRs);
    }


    @ApiOperation("增加或修改学生信息")
    @PostMapping("/addOrUpdateStudent")
    public Result addOrUpdateStudent(@RequestBody Student student){
        //对学生密码加密
        Integer id = student.getId();
        if (null == id || 0 ==id) {
            student.setPassword(MD5.encrypt(student.getPassword()));
        }
        //保存学生信息到数据库
        studentService.saveOrUpdate(student);
        return Result.ok();
    }

    @ApiOperation("删除学生信息")
    @DeleteMapping("/delStudentById")
    public Result delStudentById(
             @ApiParam("要删除的学生编号的JSON数组")@RequestBody List<Integer> ids
    ){
            studentService.removeByIds(ids);
            return Result.ok();
    }
}
